﻿using System;
using System.Collections.Generic;
using System.Text;

namespace IntegratedMeasuring.Utils.InstrumentParser;

public class BaseParser
{
     protected static string PraseTransName(byte[] bytes)
    {
        if (bytes.Length != 16) return string.Empty;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.Length; i++)
        {
            byte temp = bytes[i];
            if (temp == 0x00) continue;
            sb.Append(Convert.ToChar(temp));
        }
        return sb.ToString();
    }
    protected static List<string> PrasePinLabel(byte[] bytes)
    {
        List<string> list = new List<string>();
        // if (bytes.Length != 772) return list;
        for (int i = 1; i < bytes.Length / 4; i++)
        {
            StringBuilder sb = new StringBuilder();
            byte ch1 = bytes[4 * i];
            byte ch2 = bytes[4 * i + 1];
            byte ch3 = bytes[4 * i + 2];
            sb.Append(Convert.ToChar(ch1));
            sb.Append(Convert.ToChar(ch2));
            sb.Append(Convert.ToChar(ch3));
            list.Add(sb.ToString());
        }
        return list;
    }
    
    protected static List<Series> GenerateSeriesPins(byte[] pins, int byteLength)
    {
        // Lk -> Lx -> Turn -> ACR -> Zx -> DCR
        List<Series> series = new List<Series>();
        for (int i = 0; i < byteLength; i++)
        {
            int PRI = i / 120; ;
            int itemIndex = (i / 20) % 6;
            int SEC = (i / 2) % 10;
            string pin = string.Empty;
            int pin1 = pins[i + 0].ToInt();
            int pin2 = pins[i + 1].ToInt();
            if (pin1 == 0 && pin2 == 0)
            {
                pin = string.Empty;
            }
            else
            {
                pin = $"{pin1.ToString().PadLeft(2, '0')} - {pin2.ToString().PadLeft(2, '0')}";
            }
            series.Add(new Series
            {
                PRI = PRI,
                ItemIndex = itemIndex,
                SEC = SEC,
                ShortPin = pin,
            });
            i++;
        }
        return series;
    }
    protected static List<string> GenerateParallelPin(byte[] positive, byte[] nagetive, int byteLength)
    {
        List<string> allPins = new List<string>();
        for (int i = 0; i < byteLength; i++)
        {
            int PRI = i / 50;
            int SEC = (i / 5) % 10;

            string temp = string.Empty;
            StringBuilder sb = new StringBuilder();
            StringBuilder sb1 = new StringBuilder();
            sb1.Append(positive[i + 0].ToInt() == 0 ? string.Empty : $"{positive[i + 0].ToInt().ToString().PadLeft(2, '0')},");
            sb1.Append(positive[i + 1].ToInt() == 0 ? string.Empty : $"{positive[i + 1].ToInt().ToString().PadLeft(2, '0')},");
            sb1.Append(positive[i + 2].ToInt() == 0 ? string.Empty : $"{positive[i + 2].ToInt().ToString().PadLeft(2, '0')},");
            sb1.Append(positive[i + 3].ToInt() == 0 ? string.Empty : $"{positive[i + 3].ToInt().ToString().PadLeft(2, '0')},");
            sb1.Append(positive[i + 4].ToInt() == 0 ? string.Empty : $"{positive[i + 4].ToInt().ToString().PadLeft(2, '0')},");

            StringBuilder sb2 = new StringBuilder();
            sb2.Append(nagetive[i + 0].ToInt() == 0 ? string.Empty : $"{nagetive[i + 0].ToInt().ToString().PadLeft(2, '0')},");
            sb2.Append(nagetive[i + 1].ToInt() == 0 ? string.Empty : $"{nagetive[i + 1].ToInt().ToString().PadLeft(2, '0')},");
            sb2.Append(nagetive[i + 2].ToInt() == 0 ? string.Empty : $"{nagetive[i + 2].ToInt().ToString().PadLeft(2, '0')},");
            sb2.Append(nagetive[i + 3].ToInt() == 0 ? string.Empty : $"{nagetive[i + 3].ToInt().ToString().PadLeft(2, '0')},");
            sb2.Append(nagetive[i + 4].ToInt() == 0 ? string.Empty : $"{nagetive[i + 4].ToInt().ToString().PadLeft(2, '0')},");
            
            if (sb1.Length == 0 && sb2.Length == 0)
            {
                temp = string.Empty;
            }
            else
            {
                if (sb1.Length > 0)
                {
                    sb1.Remove(sb1.Length - 1, 1);
                }

                if (sb2.Length > 0)
                {
                    sb.Append(sb1.ToString()).Append(" & ");
                    sb2.Remove(sb2.Length - 1, 1);
                }
                else
                {
                    sb.Append(sb1.ToString());
                }
                sb.Append(sb2.ToString());
                temp = sb.ToString();
            }
            allPins.Add(temp);
            i++; i++; i++; i++;
        }
        return allPins;
    }
    
    protected static List<LkTestPin> GenerateLkTestPins(byte[] pins)
    {
        List<LkTestPin> testPins = new List<LkTestPin>();
        for (int i = 0; i < pins.Length; i++)
        {
            int PRI = i / 10;
            int SEC = i % 10;
            int pin = pins[i].ToInt();
            testPins.Add(new LkTestPin
            {
                PRI = PRI,
                SEC = SEC,
                Pin = pin,
            });
        }
        return testPins;
    }
    protected static string GenerateCxPin(byte[] CxPins)
    {
        StringBuilder sb = new StringBuilder();
        StringBuilder sb1 = new StringBuilder();
        sb1.Append(CxPins[0].ToInt() == 0 ? string.Empty : $"{CxPins[0].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[1].ToInt() == 0 ? string.Empty : $"{CxPins[1].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[2].ToInt() == 0 ? string.Empty : $"{CxPins[2].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[3].ToInt() == 0 ? string.Empty : $"{CxPins[3].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[4].ToInt() == 0 ? string.Empty : $"{CxPins[4].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[5].ToInt() == 0 ? string.Empty : $"{CxPins[5].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[6].ToInt() == 0 ? string.Empty : $"{CxPins[6].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[7].ToInt() == 0 ? string.Empty : $"{CxPins[7].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[8].ToInt() == 0 ? string.Empty : $"{CxPins[8].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[9].ToInt() == 0 ? string.Empty : $"{CxPins[9].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[10].ToInt() == 0 ? string.Empty : $"{CxPins[10].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[11].ToInt() == 0 ? string.Empty : $"{CxPins[11].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[12].ToInt() == 0 ? string.Empty : $"{CxPins[12].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[13].ToInt() == 0 ? string.Empty : $"{CxPins[13].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[14].ToInt() == 0 ? string.Empty : $"{CxPins[14].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[15].ToInt() == 0 ? string.Empty : $"{CxPins[15].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[16].ToInt() == 0 ? string.Empty : $"{CxPins[16].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(CxPins[17].ToInt() == 0 ? string.Empty : $"{CxPins[17].ToInt().ToString().PadLeft(2, '0')},");

        StringBuilder sb2 = new StringBuilder();
        sb2.Append(CxPins[18].ToInt() == 0 ? string.Empty : $"{CxPins[18].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[19].ToInt() == 0 ? string.Empty : $"{CxPins[19].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[20].ToInt() == 0 ? string.Empty : $"{CxPins[20].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[21].ToInt() == 0 ? string.Empty : $"{CxPins[21].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[22].ToInt() == 0 ? string.Empty : $"{CxPins[22].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[23].ToInt() == 0 ? string.Empty : $"{CxPins[23].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[24].ToInt() == 0 ? string.Empty : $"{CxPins[24].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[25].ToInt() == 0 ? string.Empty : $"{CxPins[25].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[26].ToInt() == 0 ? string.Empty : $"{CxPins[26].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[27].ToInt() == 0 ? string.Empty : $"{CxPins[27].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[28].ToInt() == 0 ? string.Empty : $"{CxPins[28].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[29].ToInt() == 0 ? string.Empty : $"{CxPins[29].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[30].ToInt() == 0 ? string.Empty : $"{CxPins[30].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[31].ToInt() == 0 ? string.Empty : $"{CxPins[31].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[32].ToInt() == 0 ? string.Empty : $"{CxPins[32].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[33].ToInt() == 0 ? string.Empty : $"{CxPins[33].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[34].ToInt() == 0 ? string.Empty : $"{CxPins[34].ToInt().ToString().PadLeft(2, '0')},");
        sb2.Append(CxPins[35].ToInt() == 0 ? string.Empty : $"{CxPins[35].ToInt().ToString().PadLeft(2, '0')},");


        if (sb1.Length == 0 && sb2.Length == 0)
        {
            return "N/A";
        }
        else
        {
            if (sb1.Length > 0)
            {
                sb1.Remove(sb1.Length - 1, 1);
            }

            if (sb2.Length > 0)
            {
                sb.Append(sb1.ToString()).Append(" - ");
                sb2.Remove(sb2.Length - 1, 1);
            }
            else
            {
                sb.Append(sb1.ToString());
            }
            sb.Append(sb2.ToString());
            return sb.ToString();
        }
    }

    protected static string GenerateCxShortPin(byte[] shortPins)
    {
        StringBuilder sb1 = new StringBuilder();
        sb1.Append(shortPins[0].ToInt() == 0 ? string.Empty : $"{shortPins[0].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(shortPins[1].ToInt() == 0 ? string.Empty : $"{shortPins[1].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(shortPins[2].ToInt() == 0 ? string.Empty : $"{shortPins[2].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(shortPins[3].ToInt() == 0 ? string.Empty : $"{shortPins[3].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(shortPins[4].ToInt() == 0 ? string.Empty : $"{shortPins[4].ToInt().ToString().PadLeft(2, '0')},");
        sb1.Append(shortPins[5].ToInt() == 0 ? string.Empty : $"{shortPins[5].ToInt().ToString().PadLeft(2, '0')},");

        if (sb1.Length == 0)
        {
            return "N/A";
        }
        else
        {
            sb1.Remove(sb1.Length - 1, 1);
            return sb1.ToString();
        }
    }
    protected static string GenerateLkPin(byte[] LkPins)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(LkPins[0].ToInt() == 0 ? string.Empty : $"{LkPins[0].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[1].ToInt() == 0 ? string.Empty : $"{LkPins[1].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[2].ToInt() == 0 ? string.Empty : $"{LkPins[2].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[3].ToInt() == 0 ? string.Empty : $"{LkPins[3].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[4].ToInt() == 0 ? string.Empty : $"{LkPins[4].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[5].ToInt() == 0 ? string.Empty : $"{LkPins[5].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[6].ToInt() == 0 ? string.Empty : $"{LkPins[6].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[7].ToInt() == 0 ? string.Empty : $"{LkPins[7].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[8].ToInt() == 0 ? string.Empty : $"{LkPins[8].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[9].ToInt() == 0 ? string.Empty : $"{LkPins[9].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[10].ToInt() == 0 ? string.Empty : $"{LkPins[10].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[11].ToInt() == 0 ? string.Empty : $"{LkPins[11].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[12].ToInt() == 0 ? string.Empty : $"{LkPins[12].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[13].ToInt() == 0 ? string.Empty : $"{LkPins[13].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[14].ToInt() == 0 ? string.Empty : $"{LkPins[14].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[15].ToInt() == 0 ? string.Empty : $"{LkPins[15].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[16].ToInt() == 0 ? string.Empty : $"{LkPins[16].ToInt().ToString().PadLeft(2, '0')},");
        sb.Append(LkPins[17].ToInt() == 0 ? string.Empty : $"{LkPins[17].ToInt().ToString().PadLeft(2, '0')},");
        
        if (sb.Length == 0)
        {
            return "N/A";
        }
        else
        {
            sb.Remove(sb.Length - 1, 1);
            return sb.ToString();
        }
    }
}